#include "data_io.h"

#include <iostream>
#include <sstream>
#include <typeinfo>

///body file for class Data_IO

int Data_IO::get_natural(std::istream &p_in_stream) throw(File_IO_exceptions)
{
	const bool p_from_std_cin=(typeid(p_in_stream)==typeid(std::cin));
	///different input procedure when reading from std::cin or from something else
  bool ok=true;
  int n;
	std::string p_string;
	do
	{
		getline(p_in_stream,p_string);
		std::istringstream* p_s;
		p_s=new std::istringstream(p_string);
		(*p_s)>>std::noskipws>>n;
		delete p_s;
		if((0==n && "0"!=p_string) || n<0)
		{
			ok=false;
			if(p_from_std_cin)
			{
				///simply make user to give a correct value
				std::cerr<<" * Adjon meg egy termeszetes szamot: "<<std::endl;
			}
			else
			{
				///unable to make user to give a correct value
				File_IO_exceptions number_error("Hibas szamadat az adatfajlban.");
				std::cerr<<number_error.what()<<std::endl;
				throw number_error;
			}
		}
		else
		{
			ok=true;
		}
	} while(!ok);
	return(n);
}

std::string Data_IO::get_string(std::istream &p_in_stream) throw(File_IO_exceptions)
{
	const bool p_from_std_cin=(typeid(p_in_stream)==typeid(std::cin));
	///different input procedure when reading from std::cin or from something else
	bool ok=true;
	std::string p_string;
	do
	{
		getline(p_in_stream,p_string);
		if(p_string.empty())
		{
			ok=false;
			if(p_from_std_cin)
			{
				///simply make user to give a correct value
				std::cerr<<" * Adjon meg egy szoveget: "<<std::endl;
			}
			else
			{
				///unable to make user to give a correct value
				File_IO_exceptions string_error("Hibas szovegadat az adatfajlban.");
				std::cerr<<string_error.what()<<std::endl;
				throw string_error;
			}
		}
		else
		{
			ok=true;
		}
	}while(!ok);
	return(p_string);
}
